﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.HousekeepingAndStaffMngmt
{
    public partial class ItemManagementEdit : Form
    {
        public ItemManagementEdit(string Type, string Value)
        {
            InitializeComponent();

            //Defining what the user search by
            if (Type.Equals("Item ID"))
            {
                int value2 = Convert.ToInt32(Value);
                Item item = HousekeepingDBManager.GetItemByID(value2);

                lblItemID2.Text = item.itemID.ToString();
                tbxItemName.Text = item.itemName.ToString();
                tbxItemPrice.Text = item.itemPrice.ToString();

            }
            else if (Type.Equals("Item Name"))
            {
                Item item = HousekeepingDBManager.GetItemByName(Value);

                lblItemID2.Text = item.itemID.ToString();
                tbxItemName.Text = item.itemName.ToString();
                tbxItemPrice.Text = item.itemPrice.ToString();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to update this item?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                //Declaring and storing into variables
                int itemID = Convert.ToInt32(lblItemID2.Text);
                string itemName = tbxItemName.Text;
                double itemPrice = Convert.ToDouble(tbxItemPrice.Text);

                #region check for existing item and update item
                //Check for existing item
                ArrayList allItem = HousekeepingDBManager.GetAllItemList();
                bool insert = true;
                foreach (Item item2 in allItem)
                {
                    if (item2.itemName.Equals(itemName))
                    {
                        MessageBox.Show("Item name already exists!");
                        insert = false;
                        break;
                    }
                }

                //If item doesnt exist, update the item
                if (insert == true)
                {
                    int success = HousekeepingDBManager.UpdateItemByID(itemID, itemName, itemPrice);

                    if (success == 1)
                    {
                        MessageBox.Show("Item updated successfully!");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Item not updated, please try again!");
                    }
                }
                #endregion

            }
        }

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // Validation of user input
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        private void tbxItemName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxItemName.Text))
            {
                MessageBox.Show("Item name required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxItemName.SelectAll();
                tbxItemName.Focus();
                return;
            }
        }
        private void tbxItemPrice_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxItemPrice.Text))
            {
                MessageBox.Show("Item price required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxItemPrice.SelectAll();
                tbxItemPrice.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxItemPrice.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Item price invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxItemPrice.SelectAll();
                tbxItemPrice.Focus();
                return;
            }
        }
    }
}
